package com.bawei.detail.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.common.vm.BaseViewModel
import com.bawei.detail.intent.DetailViewIntent
import com.bawei.detail.model.DetailRepository
import com.bawei.detail.model.MovieDetailPagingSource
import com.bawei.detail.uistate.DetailUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: Jason Zhang
 * Date: 2024/3/9
 * Desc:
 */
@HiltViewModel
class DetailViewModel @Inject constructor(private val repository: DetailRepository):BaseViewModel<DetailViewIntent>() {

    private var _uiState  = MutableStateFlow<DetailUiState>(DetailUiState.LoadEmpty)
    val uiState: StateFlow<DetailUiState> = _uiState.asStateFlow()

    override fun dispatchIntent(intent: DetailViewIntent) {
        when(intent){
            is DetailViewIntent.LoadAuthorInfo -> loadAuthorInfo(intent.userId)
            is DetailViewIntent.LoadMovieCommend -> loadMovieCommend(intent.dataType,intent.itemId)
            else -> {}
        }
    }

    private fun loadMovieCommend(dataType: Int, itemId: String) {
        viewModelScope.launch {
            try {
                val (code, data, msg) = repository.getMovieCommendById(dataType, itemId)
                if (code == 0 && data.isNotEmpty()) {
                    _uiState.value = DetailUiState.LoadMovieCommendSuccess(data)
                } else {
                    _uiState.value = DetailUiState.LoadMovieCommendFail(msg)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                _uiState.value = DetailUiState.LoadMovieCommendFail("网络加载错误")
            } finally {
                this.cancel()
            }
        }
    }

    private fun loadAuthorInfo(userId: String) {
        viewModelScope.launch {
            try {
                val (code, data, msg) = repository.getAuthorInfoById(userId)
                if(code == 0){
                    _uiState.value = DetailUiState.LoadAuthorInfoSuccess(data)

                }else{
                    _uiState.value = DetailUiState.LoadAuthorInfoFail(msg)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                _uiState.value = DetailUiState.LoadAuthorInfoFail("网络加载错误")
            } finally {
                this.cancel()
            }
        }

    }

    fun loadRecommendMovie(): Flow<PagingData<MovieVideoEntity>> {
        return Pager(config = PagingConfig(
            pageSize = 10,
            initialLoadSize = 10,
            enablePlaceholders = false),
            pagingSourceFactory = { MovieDetailPagingSource() }
            ).flow.cachedIn(viewModelScope)
    }

    fun loadRecommendMovieById(typeId: Int): Flow<PagingData<MovieVideoEntity>> {
        return Pager(config = PagingConfig(
            pageSize = 10,
            initialLoadSize = 10,
            enablePlaceholders = false),
            pagingSourceFactory = { MovieDetailPagingSource(typeId) }
        ).flow.cachedIn(viewModelScope)
    }


}